#!/usr/bin/env python3

import pandas as pd
from qiutil import INVALID_STOCKS
# 最近一个月末，对R和E分别进行描述性统计，画出直方图

class RoeFactor:
    def __init__(self, raw):
        # 净收入负值过大：国航 601111.SH -10321667000.0
        #               温氏股份 深创业 300498.SZ -9,701,319,910.97
        # print(income_data_sep["NET_PROFIT_GM"].min())
        # 归母权益负值： 神州细胞 688520.SH -33,380,669.92
        query_str = 'I_RPT_PERIOD > "20210830" & ' \
            f'STOCK_CODE != {INVALID_STOCKS}'
        self.sep_data = raw.income_data.query(query_str)

        self.sep_data = pd.merge(self.sep_data, raw.ind1k8, how='inner',
                                 on=["STOCK_CODE", "STOCK_CODE"])
        # 最近一个月末，计算所有股票的ROE，对ROE进行描述性统计，画出直方图，
        # 观察极端值，讨论造成极端值的原因和极端值的处理方式
        self.mrt_latest = raw.mrt.query(
            'TRADE_DATE > "20211030" & STOCK_CODE != "688520.SH"')

        self.mrt_latest = pd.merge(self.mrt_latest, raw.ind1k8, how='inner',
                                   on=["STOCK_CODE", "STOCK_CODE"])
        self.raw = raw

    def roe_ttm(self):
        # 定义：最近12个月的归母净利润总和，除归母权益最近四个报告期的均值
        #self.net_income_parent_ttm
        pass


    def net_profit_gm_ttm(self):
        # 1. check current Q, figure out
        # how many Quaters of last year needs to be counted in
        # 2.
        pass

    def roe_ttm_stock_series(self, stock_code):
        return self.roe_ttm.query(f'STOCK_CODE=="{stock_code}"')

    def plot(self):
        print("min net profit gm: " + str(self.sep_data["NET_PROFIT_GM"].min()))
        print("Shape of income data in September: " + str(self.sep_data.shape))
        print(self.sep_data.head(5))
